# coding=utf-8
from sutras import enums, models, serializers


def get_order_list():
    order_objs = models.PrintOrder.objects.exclude(
        status=enums.PrintOrderStatus.deleted)
    order_list = serializers.PrintOrderList(order_objs, many=True).data
    return order_list


def delete_order(order_id):
    models.PrintOrder.objects.filter(
        pk=order_id).update(status=enums.PrintOrderStatus.deleted)


def update_order(order_id, remark):
    models.PrintOrder.objects.filter(pk=order_id).update(remark=remark)


def get_order_detail(order_id):
    order = models.PrintOrder.objects.filter(pk=order_id).first()
    if not order:
        return None
    order_data = {
        # 基本信息
        "id": order.id,
        "serial_num": order.serial_num,
        "uid": order.user.uid,
        "nickname": order.user.nickname,
        "email": order.user.email,
        "phone": order.user.phone,
        "total_should_pay": order.total_should_pay,
        "status": order.status,
        "create_time": order.create_time,
        "pay_time": order.pay_time,
        "pay_method": order.pay_method,
        # 商品信息
        "art_no": order.sutra_edition.art_no,
        "print_price": order.sutra_edition.print_price,
    }
    deliver_detail = models.PrintOrderDetail.objects.filter(
        print_order=order, contact__isnull=False).first()
    if deliver_detail:
        # 收货信息
        deliver_info = {
            "contact": deliver_detail.contact,
            "contact_phone": deliver_detail.phone,
            "contact_address": deliver_detail.address,
            "contact_postcode": deliver_detail.postcode,
            "num": deliver_detail.num,
        }
        order_data.update(deliver_info)
    return order_data
